package com.presalept.manipulatedata;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import com.presalept.polodo.*;

public class UpdateEventandUser {
	
	public static int UpdateEventUser(EventandUserDO eventanduser) throws SQLException{
		
		int UpdateResult = 0;
		Connection dbConn = null;
		PreparedStatement dbEventUserStmt = null;
		ResultSet dbRs = null;
	
		String sqlUpdateEventUser = "UPDATE relationofeventanduser SET eurelationstatus = '0' WHERE userid = ? and projectid = ?";
				
		try{
			
			dbConn = DbManager.getConnection();		
			
			dbEventUserStmt = dbConn.prepareStatement(sqlUpdateEventUser);
			
			//update the eurelationstatus when the user has read the record;

				
			dbEventUserStmt.setInt(1, eventanduser.getUserId());
			dbEventUserStmt.setInt(2, eventanduser.getProjectId());
			
			UpdateResult = dbEventUserStmt.executeUpdate();
			
		}finally{
			if (dbRs != null) dbRs.close();
			if (dbConn != null) dbConn.close();			
		}
		
		return UpdateResult;
	}
	
	public static int UpdateEventUser(Integer projectid, Integer userid) throws SQLException{
		
		int UpdateResult = 0;
		Connection dbConn = null;
		PreparedStatement dbEventUserStmt = null;
		ResultSet dbRs = null;
	
		String sqlUpdateEventUser = "UPDATE relationofeventanduser SET eurelationstatus = '0' WHERE userid = ? and projectid = ?";
				
		try{
			
			dbConn = DbManager.getConnection();		
			
			dbEventUserStmt = dbConn.prepareStatement(sqlUpdateEventUser);
			
			//update the eurelationstatus when the user has read the record;

				
			dbEventUserStmt.setInt(1, userid);
			dbEventUserStmt.setInt(2, projectid);
			
			UpdateResult = dbEventUserStmt.executeUpdate();
			
		}finally{
			if (dbRs != null) dbRs.close();
			if (dbConn != null) dbConn.close();			
		}
		
		return UpdateResult;
	}

	public static int UpdateEventUser(List<EventandUserDO> listeventuser) throws SQLException{
		
		int UpdateResult = 0;
		Connection dbConn = null;
		PreparedStatement dbEventUserStmt = null;
		ResultSet dbRs = null;
		int[] updateEventUserResult;
	
		String sqlUpdateEventUser = "UPDATE relationofeventanduser SET eurelationstatus = '0' WHERE userid = ? and projectid = ?";
				
		try{
			
			dbConn = DbManager.getConnection();		
			
			dbEventUserStmt = dbConn.prepareStatement(sqlUpdateEventUser);
			
			//update the eurelationstatus when the user has read the record;
			
			for(Iterator<EventandUserDO> i = listeventuser.iterator(); i.hasNext();){
				
				EventandUserDO eventanduser = i.next();
				
				dbEventUserStmt.setInt(1, eventanduser.getUserId());
				dbEventUserStmt.setInt(2, eventanduser.getProjectId());
				
				dbEventUserStmt.addBatch();		
			}
			
			updateEventUserResult = dbEventUserStmt.executeBatch();

			boolean isUpdateRight = false;
			for(int i = 0; i < updateEventUserResult.length; i++){
					
				//if some record has update error, then...
					
					if(updateEventUserResult[i] == 0){

						break;
					}
					
					isUpdateRight = true;
				}
			
			if(isUpdateRight){
					
				UpdateResult = 1;
				
			}
			
		}finally{
			if (dbRs != null) dbRs.close();
			if (dbConn != null) dbConn.close();			
		}
		
		return UpdateResult;
	}

}
